#!/bin/bash
#
#
#   goenv
#   Copyright (c)2011   Joshua Bell
#   All rights reserved.
#
#
#   This script is to be used in order to set up
#   one's environment for Golang development.
#


function _error() {
    echo "!!! $*"
}

function _basename() {
    cur_pwd=`pwd`
    echo $(basename "$cur_pwd")
}

function deactivate() {
    if [ -n "$_PREV_PS1" ]; then
        export PS1="$_PREV_PS1"
        unset _PREV_PS1
    fi

    if [ -n "$_PREV_PATH" ]; then
        export PATH="$_PREV_PATH"
        unset _PREV_PATH
    fi

    if [ -n "$BASH" ]; then
        hash -r
    fi

    unset GOOS
    unset GOARCH
    unset GOROOT

    ##  Deactivate functions
    unset _error
    unset _basename
    unset _makefile
    unset _cmdmakefile
    unset _pkgmakefile
    unset _projmakefile
    unset _mainfile
    unset startproject
    unset addsubpkg
    unset goclean
    unset goremake
    unset deactivate
}

function _makefile() {
    targ="$1"
    file="$2"
    if [ ! "$targ" ]; then
        targ=$(_basename)
    fi
    if [ ! "$file" ]; then
        file="Makefile"
    fi
cat > $file <<EOF
include \$(GOROOT)/src/Make.inc

TARG=$targ
EOF
}

function _cmdmakefile() {
    targ="$1"
    file="$2"
    if [ ! "$targ" ]; then
        targ=$(_basename)
    fi
    if [ ! "$file" ]; then
        file="Makefile"
    fi
    _makefile "$targ" "$file"
    echo "include \$(GOROOT)/src/Make.cmd" >> "$file"
cat >> "$file" <<EOF
GOFILES=\\
        main.go \\

include \$(GOROOT)/src/Make.cmd
EOF
}

function _pkgmakefile() {
    targ="$1"
    file="$2"
    if [ ! "$targ" ]; then
        targ=$(_basename)
    fi
    if [ ! "$file" ]; then
        file="Makefile"
    fi
    _makefile "$targ" "$file"
cat >> "$file" <<EOF
GOFILES=\\
        $targ.go \\

include \$(GOROOT)/src/Make.pkg
EOF
}

function _projmakefile() {
    targ="$1"
    pkg="$2"
    if [ ! "$targ" ]; then
        _error "Project makefile requires a target name"
    elif [ ! "$pkg" ]; then
        _error "Project makefile requires a package"
    else

cat > Makefile <<EOF
TARG=$targ

all:
    \$(MAKE) -C $pkg
    \$(MAKE) Makefile.\$(TARG)

clean:
    \$(MAKE) -C $pkg clean
    \$(MAKE) Makefile.\$(TARG) clean
EOF
    fi
}

function _mainfile() {
cat > main.go <<EOF
package main

import (
)

func main() {
}
EOF
}

function startproject() {
    name="$1"
    cur_dir=`pwd`
    if [ ! "$name" ]; then
        _error "Project name required to start a project"
    elif [ -d "$name" ]; then
        _error "Project with that name already exists"
    else
        mkdir -p "$name"
        cd "$name"
        _cmdmakefile "$name"
        cd "$cur_dir"
        echo "Project successfully created"
    fi
}

function addsubpkg() {
    name="$1"
    project=$(_basename)
    if [ ! "$name" ]; then
        _error "Adding a sub pkg requires a name for the pkg"
    elif [ ! "$project" ]; then
        _error "Some how we can't determine the project name"
    elif [ -d "$name" ]; then
        _error "Pkg with that name already exists"
    elif [ ! -e "Makefile.$project" ]; then
        if [ ! -e "Makefile" ]; then
            _error "Invalid project"
        fi
        mv "Makefile" "Makefile.$project"
        _projmakefile "$project" "$name"
    fi
    cur_pwd=`pwd`
    mkdir "$name"
    cd "$name"
    _pkgmakefile "$name"
    cd "$cur_pwd"
    echo "Sub pkg added successfully"
}

function goclean() {
    gomake clean
}

function goremake() {
    goclean && gomake
}

if [ "$1" ]; then
    GOROOT="$1"
elif [ -d /usr/local/go ]; then
    GOROOT=/usr/local/go
elif [ -d ~/go ]; then
    GOROOT=~/go
fi
export GOROOT


export _PREV_PATH="$PATH"
export _PREV_PS1="$PS1"


export PATH="$GOROOT/bin:$PATH"
export PS1="[go]$PS1"

#   Pull the env settings from Make.inc
eval $($GOROOT/bin/gomake -f $GOROOT/src/Make.inc go-env)
